﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.qbengo.utility.nodepicker.DAL;
using com.qbengo.utility.nodepicker.business;
using com.qbengo.utility.nodepicker.entity;

namespace com.qbengo.utility.nodepicker
{
    public partial class VenueInfoForm : Form
    {
        public int current_event_id = -1;
        public int current_map_id = -1;
        private int current_venue_id=-1;

        private bool blnLoad = true;
        VenueBLL venueBLL = new VenueBLL();
        MapBLL mapBLL = new MapBLL();
        EventBLL eventBLL = new EventBLL();
        ExhibitorBLL exhibitorBLL = new ExhibitorBLL();

        EventInfoForm eventInfoForm;
        

        public VenueInfoForm()
        {
            InitializeComponent();
            
        }

        private void VenueInfoForm_Load(object sender, EventArgs e)
        {

            //this.comboBox1.SelectedValueChanged += new System.EventHandler(comboBox1_SelectedValueChanged);
            
            
            List<Venue> list = venueBLL.GetVenuelList("");
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = list;
            this.VenueDataGrid.DataSource = bindingSource;
            this.blnLoad = false;


            //this.refreshVenue((int)this.comboBox1.SelectedValue);

            //this.dataGridView1.data
            //comboBox1.DisplayMember = "name";
            //comboBox1.ValueMember = "venueid";
            //---bindingSource.EndEdit();

        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (!this.blnLoad)
            {
                //this.refreshVenue((int)comboBox1.SelectedValue);                
            }
        }

        private void refreshVenueId(int venue_id) {

            BindingSource mapBindingSource = new BindingSource();
            List<Map> mapList = mapBLL.DataTableToList(mapBLL.GetList("venueid=" + venue_id).Tables[0]);//List<Map> mapList = mapBLL.DataTableToList(mapBLL.GetList("venueid="+venue_id).Tables[0]);
            mapBindingSource.DataSource = mapList;
            this.MapDataGrid.DataSource = mapBindingSource;

            
            BindingSource eventBindingSource = new BindingSource();
            List<Event> eventList = eventBLL.DataTableToList(eventBLL.GetList("venueid=" + venue_id).Tables[0]);
            eventBindingSource.DataSource = eventList;
            this.EventDataGrid.DataSource = eventBindingSource;
        }

        private void refreshEventId(int event_id) {
            BindingSource exhibitorBindingSource = new BindingSource();
            List<Exhibitor> exhibitorList = exhibitorBLL.DataTableToList(exhibitorBLL.GetList("eventid=" + event_id).Tables[0]);
            exhibitorBindingSource.DataSource = exhibitorList;
            this.ExhibitorDataGrid.DataSource = exhibitorBindingSource;
            
        }
        /// <summary>
        /// refresh MapDataGrid
        /// </summary>
        /// <param name="venue_id"></param>
        private void refreshMapId(int venue_id) {

           // MessageBox.Show(map_id + "");

            BindingSource mapBindingSource = new BindingSource();
            List<Map> mapList = mapBLL.DataTableToList(mapBLL.GetList("venueid=" + venue_id).Tables[0]);
            mapBindingSource.DataSource = mapList;
            this.MapDataGrid.DataSource = mapBindingSource;

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
            //MessageBox.Show(MapDataGrid.Columns[e.ColumnIndex].Name);
            
            //this.refreshMapId((int)MapDataGrid.Rows[e.RowIndex].Cells["mapid"].Value);
            
        }
        /// <summary>
        /// click VenueDataGrid_cell
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VenueDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            current_venue_id = (int)VenueDataGrid.Rows[e.RowIndex].Cells["venueid"].Value;
            this.refreshVenueId(current_venue_id);
        }
        /// <summary>
        ///  click EventDataGrid_cell
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventDataGrid_CellClick(object sender, DataGridViewCellEventArgs e) {
            //this.refreshEventId((int)EventDataGrid.Rows[e.RowIndex].Cells["eventid"].Value);
            //this.current_event_id = (int)EventDataGrid.Rows[e.RowIndex].Cells["eventid"].Value;

        }

        private void btn_event_setup_Click(object sender, EventArgs e)
        {
            eventInfoForm = new EventInfoForm();
            eventInfoForm.Owner = this;
            eventInfoForm.ShowDialog();
        }

        private void btn_venue_add_Click(object sender, EventArgs e)
        {

                AddVenue addVenue=new AddVenue();
                addVenue.Show();
                addVenue.accept += new EventHandler(AddVenue_accept);
        }

        private void AddVenue_accept(object sender, EventArgs e)
        {
            AddVenue addVenue = (AddVenue)sender;

            BindingSource bindingSource = (BindingSource)VenueDataGrid.DataSource;
               
            Venue venue = new Venue();
            venue.name = addVenue.VenueName;

            //venueBLL.Add(venue);
            //List<Venue> list = venueBLL.GetVenuelList("");

            //venue=list.Last();

            bindingSource.Add(venue);

        }

        private void btn_venue_delete_Click(object sender, EventArgs e)
        {
            Button deleteButton = (Button)sender;
            BindingSource bindingSource;

            int itemID;
            if (deleteButton.Name == "btn_venue_delete")
            {
                if (VenueDataGrid.CurrentRow == null)
                {
                    MessageBox.Show("Please select a row!");
                    return;
                }

                bindingSource = (BindingSource)VenueDataGrid.DataSource;
                itemID = ((Venue)bindingSource.Current).venueid;

                bindingSource.RemoveCurrent();

                //venueBLL.Delete(itemID);
 
            }
            if (deleteButton.Name == "btn_map_delete")
            {
                if (MapDataGrid.CurrentRow == null)
                {
                    MessageBox.Show("Please select a row!");
                    return;
                }
                bindingSource = (BindingSource)MapDataGrid.DataSource;
                itemID = ((Map)bindingSource.Current).mapid;
                
                bindingSource.RemoveCurrent();
               
                mapBLL.Delete(itemID);
 
            }
            if (deleteButton.Name == "btn_event_delete")
            {
                if (EventDataGrid.CurrentRow == null)
                {
                    MessageBox.Show("Please select a row!");
                    return;
                }
                bindingSource = (BindingSource)EventDataGrid.DataSource;
                itemID = ((Event)bindingSource.Current).eventid;

                bindingSource.RemoveCurrent();
                eventBLL.Delete(itemID);
            }

        }

        private void btn_map_setup_Click(object sender, EventArgs e)
        {
            DataGridViewRow dataGridRow;
            if (MapDataGrid.CurrentRow == null)
            {
                MessageBox.Show("Please select a row!");
                return;
            }
            dataGridRow = MapDataGrid.CurrentRow;
            NodePick nodePick = new NodePick((int)dataGridRow.Cells[0].Value);

            nodePick.Show();
        }

        private void btn_venue_apply_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            DataTable dataTable;
            BindingSource bindingSource;
            if (button.Name == "btn_venue_apply")
            {
                dataTable = (DataTable)VenueDataGrid.DataSource;
                List<Venue> venueList = venueBLL.DataTableToList(dataTable);
          //      venueBLL.UpdateList(venueList);

            }

            if (button.Name == "btn_map_apply")
            {
                bindingSource =(BindingSource)MapDataGrid.DataSource;
                List<Map> mapList = (List<Map>)bindingSource.DataSource;
                mapBLL.UpdateList(mapList);

            }
        }

        private void btn_map_add_Click(object sender, EventArgs e)
        {
            if (current_venue_id == -1)
            {
                MessageBox.Show("Please select a  venue!");
                return;
            }
            AddMap addMap = new AddMap();
            addMap.Show();
            addMap.accept += new EventHandler(AddMap_accept);
        }
        private void AddMap_accept(object sender, EventArgs e)
        {
            AddMap addMap = (AddMap)sender;
            BindingSource bindingSource = (BindingSource)MapDataGrid.DataSource;

            Map map = new Map();
            map.name = addMap.MapName;

            map.venueid = current_venue_id;
            
            mapBLL.Add(map);

            List<Map> mapList = mapBLL.DataTableToList(mapBLL.GetList("venueid=" + current_venue_id).Tables[0]);

            map=mapList.Last();
            bindingSource.Add(map);
        }

        private void btn_event_add_Click(object sender, EventArgs e)
        {
            if (current_venue_id == -1)
            {
                MessageBox.Show("Please select a  venue!");
                return;
            }
            AddEvent addevent = new AddEvent();
            addevent.Show();
            addevent.accept += new EventHandler(AddEvent_accept);
        }

        private void AddEvent_accept(object sender, EventArgs e)
        {
            AddEvent addEvent = (AddEvent)sender;
            BindingSource bindingSource = (BindingSource)EventDataGrid.DataSource;
            Event newEvent = new Event();
            newEvent.name = addEvent.EventName;
            newEvent.venueid = current_venue_id;

            eventBLL.Add(newEvent);

            List<Event> eventList = eventBLL.DataTableToList(eventBLL.GetList("venueid=" + current_venue_id).Tables[0]);

            newEvent = eventList.Last();

            bindingSource.Add(newEvent);
        }
        /// <summary>
        /// click btn_venue_refresh
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_venue_refresh_Click(object sender, EventArgs e)
        {
            VenueInfoForm_Load(sender, e);

        }
        /// <summary>
        /// click btn_map_refresh_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_map_refresh_Click(object sender, EventArgs e)
        {
            if(current_venue_id==-1)
                return;

            this.refreshMapId(current_venue_id);
        }
        /// <summary>
        /// MapDataGrid_CellValueChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MapDataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dataGridViewRow =MapDataGrid.CurrentRow;
            Map map=(Map)dataGridViewRow.DataBoundItem;
            this.mapBLL.Update(map);
        }
        /// <summary>
        /// EventDataGrid_CellValueChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventDataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dataGridViewRow = EventDataGrid.CurrentRow;
            Event updateEvent = (Event)dataGridViewRow.DataBoundItem;
            this.eventBLL.Update(updateEvent);
        }
        /// <summary>
        /// VenueDataGrid_CellValueChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VenueDataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dataGridViewRow = VenueDataGrid.CurrentRow;
            Venue updateVenue = (Venue)dataGridViewRow.DataBoundItem;
            //this.venueBLL.Update(updataVenue);
        }

    }
}
